package com.latvia.ui;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.latvia.cookingapp.R;

public class RecipePhasesActivity extends Activity {

	public TextView countDonwnTextView;
	public CountDownTimer countDownTimer;
	public Button cookNowButton;
	public SeekBar progressSeekBar;
	public int initialTime = 10;
	public int currentTime = initialTime;	
	public int currentState = 0; // 0-unstarted 1-started 2-finished
	
	MediaPlayer mediaPlayer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recipe_phases);
		
		mediaPlayer = MediaPlayer.create(this, R.raw.phasedonesound);
		
		cookNowButton = (Button) findViewById(R.id.recipePhasesRecipeCookNowButton);
		cookNowButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cookNowButton.setEnabled(false);
				doCountDown();
			}
		});

		
		countDonwnTextView = (TextView) findViewById(R.id.recipePhasesRecipeCountdownTimerTextView);
		countDonwnTextView.setText("Time: " + currentTime);
		
		progressSeekBar = (SeekBar) findViewById(R.id.recipePhasesRecipeProgressBar);
		progressSeekBar.setEnabled(false);
		progressSeekBar.setMax(currentTime);
		
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("Awsome Hamburger");
		if (savedInstanceState != null){
			hadlePossibleRestartWithSavedInstance(savedInstanceState);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void hadlePossibleRestartWithSavedInstance(Bundle savedInstanceState){
		currentState = savedInstanceState.getInt("keyStateInfo");
		switch(currentState) {
		case 1:
			currentTime = savedInstanceState.getInt("keyCurrentTime");
			doCountDown();
			break;
		case 2:
			setUpFinishOrNextButton();
			break;
		}
		
	}
	
	public void setUpFinishOrNextButton(){
		currentTime = 0;
		countDonwnTextView.setText("Time: " + currentTime);
		cookNowButton.setEnabled(true);
		cookNowButton.setText("Finish");
		cookNowButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//TODO: Implement push to new screen
				Intent myIntent = new Intent(RecipePhasesActivity.this, TakePictureActivity.class);
				RecipePhasesActivity.this.startActivity(myIntent);
			}
		});
	}
	
	public void announceFinish() {
		mediaPlayer.start();
		progressSeekBar.setProgress(initialTime);
		setUpFinishOrNextButton();
		
		new AlertDialog.Builder(this)
	    .setTitle("Congratulations!")
	    .setMessage("You Finished the cooking phase!")
	    .setPositiveButton("Close", null)
		.show();
	}
	
	public void doCountDown() {
		countDownTimer = new CountDownTimer(currentTime*1000+100, 1000) {
			
			@Override
			public void onTick(long millisUntilFinished) {
				currentTime = (int)(Math.ceil(millisUntilFinished / 1000));
				countDonwnTextView.setText("Time: " + currentTime);
				progressSeekBar.setProgress((int) (initialTime - currentTime));
				currentState = 1;
			}
			
			@Override
			public void onFinish() {
				currentState = 2;
				announceFinish();
			}
		}.start();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
	    super.onSaveInstanceState(outState);
	    
	    outState.putInt("keyCurrentTime", currentTime);
	    outState.putInt("keyStateInfo", currentState);
	    if (countDownTimer != null){
	    	countDownTimer.cancel();
	    }
	}

}
